package txtpack.Work;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

public class Exam01 {
    public static void main(String[] args) {
        File target  = new File("E:\\IO流练习\\字符流\\FileRead\\2.txt");
        File dest = new File("E:\\IO流练习\\缓存流");
        sortZhuGeL(target,dest);
    }

    public static void sortZhuGeL(File target , File dest){
        File file = new File(dest,File.separator + "1.txt");
        if (!dest.getParentFile().exists()) { // 判断父级目录是否存在不存在创建父级目录
            dest.getParentFile().mkdirs();
        }
        try (FileReader fileReader = new FileReader(target);
             BufferedReader reader = new BufferedReader(fileReader);){
            String str = "";
            int i = 0;
            String[] strings = new String[9];
            while ((str = reader.readLine()) != null){
                strings[i] = str;
                i++;
            }
            Arrays.sort(strings, new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    int i1 = Integer.parseInt(o1.substring(0,o1.indexOf(".")));
                    int i2 = Integer.parseInt(o2.substring(0,o2.indexOf(".")));
                    return i1 - i2;
                }
            });
//            for (String ignored :strings
//            ) {
//                System.out.println(ignored);
//            }
            try (FileWriter fileWriter = new FileWriter(file);
                 BufferedWriter writer =new BufferedWriter(fileWriter)
            ){
                writer.write("《出师表》   诸葛亮");
                writer.newLine();
                for (String ignored :strings
                ) {
                    writer.write(ignored);
                    writer.newLine();
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
